﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Services;
using System.Web.Script.Services;
using Newtonsoft.Json;
using System.IO;
using System.Web.UI.HtmlControls;

namespace PromotionEngine
{

    public partial class WareHouse : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.Warehouse> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.Warehouse> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetAllWareHouses()
        {
            List<Model.Warehouse> lstWarehouse = new List<Model.Warehouse>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllWarehouse(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                  bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            if (dsPromotion != null)
            {

                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.Warehouse wareHouse = new Model.Warehouse();
                    wareHouse.CompanyCode = r["CompanyCode"].ToString();
                    wareHouse.CompanyName = r["CompanyName"].ToString();
                    wareHouse.WarehouseID = r["WarehouseID"].ToString();
                    wareHouse.WarehouseCode = r["WarehouseCode"].ToString();
                    wareHouse.WarehouseName = r["WarehouseName"].ToString();
                    wareHouse.CreatedUserID = r["CreatedUserID"].ToString();
                    wareHouse.Address = r["Address"].ToString();
                    lstWarehouse.Add(wareHouse);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCompany = lstWarehouse };
                return new Response(lstWarehouse, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        public static string GetWareHouses()
        {
            List<Model.Warehouse> lstWarehouse = new List<Model.Warehouse>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllWarehouse(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                  bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            if (dsPromotion != null)
            {

                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
            }
            return string.Empty;
        }
        [WebMethod]
        public static void UpdateWarehouse(string oWarehouse, bool isInsert)
        {
            Model.Warehouse wareHouse = JsonConvert.DeserializeObject<Model.Warehouse>(oWarehouse);
            if (isInsert)
            {

                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        wareHouse.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                        wareHouse.CompanyCode = tbUser.Rows[0]["CompanyCode"].ToString();
                    }
                }
            }
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonWarehouse(wareHouse.WarehouseID, wareHouse.WarehouseCode, wareHouse.WarehouseName, wareHouse.CompanyCode, wareHouse.Address,
                wareHouse.CreatedUserID, wareHouse.CreatedDate, true, ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteWarehouse(string warehouseID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteJsonWarehouse(warehouseID, ConfigurationManager.AppSettings["DATABASE_NAME"].ToString());
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
    }
}